/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package fr.loof.fonzie.keywords;


import java.util.List;

import fr.loof.fonzie.parsers.Constraint;
import fr.loof.fonzie.parsers.JPQLPersistenceQuery;
import fr.loof.fonzie.parsers.KeyWord;
import fr.loof.fonzie.parsers.Property;

public abstract class AbstractConstraint
    extends KeyWord
    implements Constraint
{

    private String operator;

    public AbstractConstraint( String operator )
    {
        this.operator = operator;
    }

    public final void process( JPQLPersistenceQuery query, List<Class> params )
    {
        Property property = query.getCurrentProperty();
        query.append( " " ).append( operator );
        declareParameters( query, params, property );
    }

    protected void declareParameters( JPQLPersistenceQuery query, List<Class> params, Property property )
    {
        declareParameter( query, params, property, property.getName() );
    }

    /**
     * Can be overriden to declare less or more parameters
     */
    protected final void declareParameter( JPQLPersistenceQuery query, List<Class> params, Property property,
                                           String name )
    {
        query.append( " :" ).append( name );
        query.add( name, property.getType() );

        Class arg = params.remove( 0 );
        property.checkParameterType( arg );
    }

}